//
//  TTDatabaseRow.m
//  Tint
//
//  Created by Kevin Wu on 2/28/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTDatabaseRow.h"
#import <objc/runtime.h>
#import "TTDatabaseResult.h"



@implementation TTDatabaseRow


#pragma mark - Accessors

@synthesize columnData = _columnData;
@synthesize databaseResult = _databaseResult;



#pragma mark - Memory

- (id)initWithDatabaseResult:(TTDatabaseResult *)result {
    self = [super init];
    if (self) {
        _columnData = [[NSMutableArray alloc] init];
        _databaseResult = result;
    }
    return self;
}

- (void)dealloc {
    TTRelease(_columnData);
    _databaseResult = nil;
    [super dealloc];
}


#pragma mark - Public

- (NSUInteger)columnIndexForName:(NSString *)columnName {
	return [_databaseResult.columnNames indexOfObject:columnName];
}


- (BOOL)boolForColumn:(NSString *)columnName {
    return [self boolForColumnAtIndex:[self columnIndexForName:columnName]];
}
- (BOOL)boolForColumnAtIndex:(NSUInteger)columnIndex {
    return ([self intForColumnAtIndex:columnIndex] != 0);
}

- (int)intForColumn:(NSString *)columnName {
    return [self intForColumnAtIndex:[self columnIndexForName:columnName]];
}
- (int)intForColumnAtIndex:(NSUInteger)columnIndex {
    return [[_columnData objectOrNilAtIndex:columnIndex] intValue];
}

- (long long)longLongForColumn:(NSString *)columnName {
    return [self longLongForColumnAtIndex:[self columnIndexForName:columnName]];
}
- (long long)longLongForColumnAtIndex:(NSUInteger)columnIndex {
    return [[_columnData objectOrNilAtIndex:columnIndex] longLongValue];
}

- (double)doubleForColumn:(NSString *)columnName {
    return [self doubleForColumnAtIndex:[self columnIndexForName:columnName]];
}
- (double)doubleForColumnAtIndex:(NSUInteger)columnIndex {
    return [[_columnData objectOrNilAtIndex:columnIndex] doubleValue];
}

- (NSString *)stringForColumn:(NSString *)columnName {
    return [self stringForColumnAtIndex:[self columnIndexForName:columnName]];
}
- (NSString *)stringForColumnAtIndex:(NSUInteger)columnIndex {
    return [_columnData objectOrNilAtIndex:columnIndex];
}

- (NSDate *)dateForColumn:(NSString *)columnName {
    return [self dateForColumnIndex:[self columnIndexForName:columnName]];
}
- (NSDate *)dateForColumnIndex:(NSUInteger)columnIndex {
    double interval = [self doubleForColumnAtIndex:columnIndex];
    if (interval > 0.0) {
        return [NSDate dateWithTimeIntervalSince1970:interval];
    }
    return nil;
}

@end




@implementation NSObject (DatabaseRow)

+ (id)objectWithDatabaseRow:(TTDatabaseRow *)row {
    if (row==nil) return nil;
    
    id object = [[self alloc] init];
    
    NSArray *propertyNames = [[self class] propertyNames];
    
    for (int i=0; i<[propertyNames count]; ++i) {
        NSString *name = [propertyNames objectAtIndex:i];
        NSString *attribute = [self attributeForProperty:name];
        
        if ([attribute hasPrefix:@"Tc"]) { // BOOL: Tc
            NSNumber *value = [NSNumber numberWithBool:[row boolForColumn:name]];
            [object setValue:value forKey:name];
        } else if ([attribute hasPrefix:@"Ti"]) { // int: Ti
            NSNumber *value = [NSNumber numberWithInt:[row intForColumn:name]];
            [object setValue:value forKey:name];
        } else if ([attribute hasPrefix:@"Tq"]) { // long long: Tq
            NSNumber *value = [NSNumber numberWithLongLong:[row longLongForColumn:name]];
            [object setValue:value forKey:name];
        } else if ([attribute hasPrefix:@"Td"]) { // double: Td
            NSNumber *value = [NSNumber numberWithDouble:[row doubleForColumn:name]];
            [object setValue:value forKey:name];
        } else if ([attribute hasPrefix:@"T@\"NSString\""]) { // NSString: T@"NSString"
            [object setValue:[row stringForColumn:name] forKey:name];
        } else if ([attribute hasPrefix:@"T@\"NSDate\""]) { // NSDate: T@"NSDate"
            [object setValue:[row dateForColumn:name] forKey:name];
        }
        
    }
    
    return [object autorelease];
}

@end
